// pages/market/order/order.js
import api from '../../../utils/api'
import message from '../../../utils/message'
Page({

    /**
     * 页面的初始数据
     */
    data: {

        order_list:[],
        show_no_data_tip:false,
        status:0,
        page_index:1,
        no_more_data:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        if( options.status ){
            this.setData({
                status:options.status
            })
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

        this.getUserShopOrderList();

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getUserShopOrderList:function (params) {
        var me = this;
        var params = {};
        params.status= this.data.status;
        
        message.show_loading();
        api.shop.getUserShopOrderList( params  , function (response) {
            message.hide_loading();

            if(response.status==1){


                var no_more_data = false;

                if( response.data.length<10 ){
                    no_more_data = true;
                }

                var list = response.data;

                for(var i=0;i<list.length;i++){
                    var a = list[i].product_info;

                    if(a!=null && a!=''){

                        list[i].products = JSON.parse( a )
                    }else{
                        list[i].products = [];
                    }
                }

                me.setData({
                    order_list: list,
                    has_load:true,
                    no_more_data:no_more_data
                })

            }
        })
    },
    selectStatus:function (e) {
        var status = e.currentTarget.dataset.status;
        this.setData({
            status: status,
            no_more_data:false,
            order_list:[],
            has_load: false
        })

        this.getUserShopOrderList();
        
    },
    getMore:function (params) {
        
    },
    doShopOrderCancel:function (e) {
        //取消店铺订单
        var me = this;

        var order_no = e.currentTarget.dataset.order_no;

        if(!order_no){
            message.show_error("订单编号异常");
        }

        var params = {};
        params.order_no = order_no;

        api.shop.doShopOrderCancel( params , function (response) {
            if(response.status==1){
                message.show_error(response.message , function () {
                    //刷新当前选中数据页
                    me.getUserShopOrderList();
                })
            }else{
                message.show_error(response);
            }
        });
        
    },
    doShopOrderPay:function( e ){
        //去支付订单
        var me = this;
        
        var order_no  = e.currentTarget.dataset.order_no;

        var params = {}
        params.order_no = order_no;

        api.shop.doShopOrderPay( params , function (response) {
            if(response.status == 1){
                me.appletPay(response.data , order_no )

            }else{
                message.show_error( response.message );
            }
        } )

    },
    appletPay:function( payInfo , order_no ){
        //调用微信支付信息，勾起订单支付
        var me = this;
        wx.requestPayment({
          "timeStamp": payInfo.timeStamp.toString(),
          "nonceStr": payInfo.nonceStr,
          "package": payInfo.package,
          "signType": payInfo.signType,
          "paySign": payInfo.paySign,
          success:function( res ){
              console.log(res);
              //me.getOrderDetail(order_no);
              //me.getOrderList(0)
              //支付完成， 校验支付结果
              message.show_error("订单支付成功，商家确认后安排配送" , function(  ){
                 me.getUserShopOrderList();
              })

              //console.log(order_no)
          },
          fail:function(res){
              console.log(res)
          }
        })
    },
    doShopOrderComplete:function (e) {
        
        var order_no = e.currentTarget.dataset.order_no;

        var me = this;
        var params = {}
        params.order_no = order_no;

        api.shop.doShopOrderComplete( params , function (response) {

            if( response.status==1 ){

                message.show_success( response.message , function () {
                    me.getUserShopOrderList();
                } )

            }else{
                message.show_error( response );

            }
            
        })
    },
    getMore:function () {
        var me = this;
        var params = {};
        var page_index = this.data.page_index;
        if(this.data.has_load){
            page_index++;
        }

        params.page_index = page_index;
        params.status = this.data.status;

        message.show_loading();
        api.shop.getUserShopOrderList( params , function (response) {
            message.hide_loading();

            if( response.status==1 ){
                var order_list = me.data.order_list;

                var no_more_data = false;
                if(response.data.length<10){
                    no_more_data = true
                }


                var list = response.data;

                for(var i=0;i<list.length;i++){
                    var a = list[i].product_info;

                    if(a!=null && a!=''){

                        list[i].products = JSON.parse( a )
                    }else{
                        list[i].products = [];
                    }
                }

                order_list = order_list.concat( list );

                me.setData({
                    order_list: order_list,
                    page_index: page_index,
                    no_more_data: no_more_data
                })
            }
        })

        
    },
    doOrderRefund:function (e) {
        var order_no  = e.currentTarget.dataset.order_no;

        if(order_no){
            wx.navigateTo({
              url: '/pages/market/refund/refund?order_no='+order_no,
            })
        }
        
        
    }
})